package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created on 2023/6/30
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    /**
     *为用户分配角色
     */
    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam Long[] roleId){
        roleService.adminToRole(adminId, roleId);
        return Result.ok(null);
    }

    /**
     * 获取用户角色
     */
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{adminId}")
    public Result getRoles(@PathVariable Long adminId){
        Map<String,Object> roleMap = roleService.toAssignByAdminId(adminId);
        return Result.ok(roleMap);
    }

    /**
     * 用户分页条件查询
     */
    @ApiOperation("用户分页条件查询")
    @GetMapping("{current}/{limit}")
    public Result getPageUserList(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  AdminQueryVo adminQueryVo){

        //处理分页参数
        Page<Admin> pageParam = new Page<>();

        //调用分页方法
        IPage<Admin> pageModel = adminService.selectPageUserList(pageParam,adminQueryVo);

        return Result.ok(pageModel);

    }

    /**
     * 根据用户id查询用户信息
     */
    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("get/{id}")
    public Result getUserById(@PathVariable Long id){
        return Result.ok(adminService.getById(id));
    }

    /**
     * 新增用户信息
     */
    @ApiOperation("新增用户")
    @PostMapping("save")
    public Result add(@RequestBody Admin admin){

        //获取用户密码
        String password = admin.getPassword();

        //使用加密工具类加密
        String passwordMd5 = MD5.encrypt(password);

        //设置加密密码
        admin.setPassword(passwordMd5);

        //开始新增操作
        return Result.ok(adminService.save(admin));
    }

    /**
     * 更新用户信息
     */
    @ApiOperation("更新用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin){
       return Result.ok(adminService.updateById(admin));
    }

    /**
     * 删除用户信息
     */
    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        return Result.ok(adminService.removeById(id));
    }

    /**
     * 批量删除用户信息
     */
    @ApiOperation("批量删除用户信息")
    @DeleteMapping("batchRemove")
    public Result deleteBatch(@RequestBody List<Long> ids){
        return Result.ok(adminService.removeByIds(ids));
    }
}
